package com.elitel.frame.base.controller;


import com.elitel.frame.base.component.RedisCacheGroupServer;
import com.elitel.frame.base.service.BaseServicesService;
import com.elitel.frame.base.service.GroupService;
import com.elitel.frame.main.entity.BaseServices;
import com.elitel.frame.main.entity.CfgGroup;
import com.elitel.frame.main.entity.ext.CfgGroupExt;
import com.elitel.frame.main.entity.vo.KeyValueModelVo;
import com.elitel.frame.main.entity.vo.ZTreeDataModelVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;



/**
 * @Description 分组管理
 * @author guoyanfei
 */
@Controller
@RequestMapping("/system/group")
public class GroupController{
	
	private static final Logger logger = LoggerFactory.getLogger(GroupController.class);
	
	@Autowired
	private GroupService groupService;
	@Autowired
	private BaseServicesService baseServicesService;
	@Autowired
	private RedisCacheGroupServer redisCacheGroupServer;
	
	
	/**
	 * @Title: getAllGroupList 
	 * @Description: TODO(查询所有分组信息) 
	 * @param 
	 * @author guoyanfei
	 * @return List<CfgGroupExt>(分组信息集合)
	 * @throws
	 */
	@RequestMapping("/getGroupList")
	@ResponseBody
	public List<CfgGroupExt> getAllGroupList(HttpServletRequest request)
	{
		List<CfgGroupExt> groupList = new ArrayList<CfgGroupExt>();
		try {
			groupList = groupService.getAllList(0);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
		}
		
		return groupList;
	}
	
	
	/**
	 * @Title: addGroupProject 
	 * @Description: TODO(新增系统) 
	 * @param sysGroupName(系统名称)、sysRemark(系统描述信息)
	 * @author guoyanfei
	 * @return int(插入结果)
	 * @throws
	 */
	@RequestMapping("/addGroupProject")
	@ResponseBody
	public int addGroupProject(HttpServletRequest request){
		int result = 0;
		try {
			String sysGroupName = request.getParameter("sysGroupName");
			String sysRemark = request.getParameter("sysRemark");
			CfgGroup cfgGroup =new CfgGroup();
			cfgGroup.setGroupname(sysGroupName);
			cfgGroup.setRemark(sysRemark);
			cfgGroup.setFatureid(0);
			result = groupService.addCfgGroup(cfgGroup);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
		}
		
		return result;
	}
	
	@RequestMapping("/updateGroupProject")
	@ResponseBody
	public int updateGroupProject(HttpServletRequest request){
		int result = 0;
		try {
			String sysGroupId = request.getParameter("sysGroupId");
			String sysGroupName = request.getParameter("sysGroupName");
			String sysRemark = request.getParameter("sysRemark");
			CfgGroup cfgGroup =new CfgGroup();
			cfgGroup.setGroupid(Integer.valueOf(sysGroupId));
			cfgGroup.setGroupname(sysGroupName);
			cfgGroup.setRemark(sysRemark);
			//cfgGroup.setFatureid(0);
			result = groupService.updateCfgGroup(cfgGroup);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
		}
		
		return result;
	}
	
	/**
	 * @Title: addGroup 
	 * @Description: TODO(新增分组功能信息) 
	 * @param CfgGroup实体
	 * @author guoyanfei
	 * @return int(插入结果)
	 * @throws
	 */
	@RequestMapping("/addGroup")
	@ResponseBody
	public int addGroup(@RequestBody CfgGroup cfgGroup){
		int result = 0;
		try {
			result = groupService.addCfgGroup(cfgGroup);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * @Title: updateGroup 
	 * @Description: TODO(修改分组功能信息) 
	 * @param CfgGroup实体
	 * @author guoyanfei
	 * @return int(修改结果)
	 * @throws
	 */
	@RequestMapping("/updateGroup")
	@ResponseBody
	public int updateGroup(@RequestBody CfgGroup cfgGroup){
		int result  = 0;
		try {
			result = groupService.updateCfgGroup(cfgGroup);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * @Title: delGroup 
	 * @Description: TODO(删除分组功能信息) 
	 * @param groupId(主键标识)
	 * @author guoyanfei
	 * @return int(修改结果)
	 * @throws
	 */
	@RequestMapping("/delGroup")
	@ResponseBody
	public int delGroup(HttpServletRequest request){
		int result = 0;
		try {
			String groupId = request.getParameter("groupId");
			result = groupService.delCfgGroup(Integer.valueOf(groupId));
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
		}
		
		return result;
	}
	
	/**
	 * @Title: getAllServiceInfo 
	 * @Description: TODO(查询所有基础服务信息) 
	 * @param 
	 * @author guoyanfei
	 * @return List<KeyValueModelVo>(keyvalue集合)
	 * @throws
	 */
	@RequestMapping("/getAllServiceInfo")
	@ResponseBody
	public List<BaseServices> getAllServiceInfo(HttpServletRequest request){
		List<BaseServices> baseServices = new ArrayList<BaseServices>();
		try {
			baseServices = baseServicesService.getAllServices();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
		}
		return baseServices;
	}
	
	/**
	 * @Title: getServiceInfoByguid 
	 * @Description: TODO(通过主键标识查询服务详细信息) 
	 * @param guid(主键标识)
	 * @author guoyanfei
	 * @return List<KeyValueModelVo>(keyvalue集合)
	 * @throws
	 */
	@RequestMapping("/getServiceInfoByguid")
	@ResponseBody
	public List<KeyValueModelVo> getServiceInfoByguid(HttpServletRequest request){
		List<KeyValueModelVo> keyValueModelVos = new ArrayList<KeyValueModelVo>();
		try {
			String guid = request.getParameter("guid");
			keyValueModelVos = groupService.getServiceList(guid);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
		}
		
		return keyValueModelVos;
		
	}
	
	/**
	 * @Title: getServiceInfoByguid 
	 * @Description: TODO(查询树形结构分组信息) 
	 * @param
	 * @author guoyanfei
	 * @return List<ZTreeDataModelVo>(zTree树形结构)
	 * @throws
	 */
	@RequestMapping("/getGroupForTree")
	@ResponseBody
	public List<ZTreeDataModelVo> getGroupForTree(){
		List<ZTreeDataModelVo> zTreeDataModelVos = new ArrayList<ZTreeDataModelVo>();
		try {
			zTreeDataModelVos = redisCacheGroupServer.getGroupListForTree(0);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
		}
		return zTreeDataModelVos;
	}
	
	
}
